
package com.ami.kvm.jviewer.hid;

import com.ami.kvm.jviewer.Debug;
import com.ami.kvm.jviewer.kvmpkts.IVTPPktHdr;
import com.ami.kvm.jviewer.kvmpkts.KMCrypt;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class USBMouseRep extends USBMessage {
	private static final int DIRABS_MAX_SCALED_X = 32767;
	private static final int DIRABS_MAX_SCALED_Y = 32767;
	public static byte INVALID_MOUSE_MODE = -1;
	public static byte RELATIVE_MOUSE_MODE = 1;
	public static byte ABSOLUTE_MOUSE_MODE = 2;
	public static byte OTHER_MOUSE_MODE = 3;

	private byte m_MouseMode = INVALID_MOUSE_MODE;
	public static byte m_btnStatus = 0;
	public double m_xDisp;
	public double m_yDisp;
	private ByteBuffer m_repBuf;
	private byte[] m_report;
	private static int SeqNum = 0;

	private double m_screenwidth;

	private double m_screenheight;

	private byte m_wheelrotation;

	public USBMouseRep() {
		this((byte) 0, 0, 0, (byte) 0);
	}

	public USBMouseRep(byte paramByte1, int paramInt1, int paramInt2, byte paramByte2) {
		m_btnStatus = paramByte1;
		this.m_xDisp = paramInt1;
		this.m_yDisp = paramInt2;
		this.m_wheelrotation = paramByte2;
	}

	public void set(byte paramByte1, double paramDouble1, double paramDouble2, byte paramByte2, int paramInt1,
			int paramInt2, byte paramByte3) {
		m_btnStatus = paramByte1;
		this.m_xDisp = paramDouble1;
		this.m_yDisp = paramDouble2;
		this.m_MouseMode = paramByte2;
		this.m_screenwidth = paramInt1;
		this.m_screenheight = paramInt2;

		if (this.m_screenwidth == 832.0D) {
			this.m_screenwidth = 800.0D;
		}
		this.m_wheelrotation = paramByte3;
	}

	public byte[] ABSreport(KMCrypt paramKMCrypt) {
		short s1 = (short) (int) (this.m_xDisp * 32767.0D / this.m_screenwidth + 0.5D);
		short s2 = (short) (int) (this.m_yDisp * 32767.0D / this.m_screenheight + 0.5D);

		if (paramKMCrypt != null) {
			this.m_vHdr = new IVTPPktHdr((short) 1, 41, (short) 0);

			this.m_report = new byte[49];
		} else {
			this.m_vHdr = new IVTPPktHdr((short) 1, 39, (short) 0);

			this.m_report = new byte[47];
		}

		int i = 7;

		this.m_repBuf = ByteBuffer.wrap(this.m_report);
		this.m_repBuf.order(ByteOrder.LITTLE_ENDIAN);
		this.m_repBuf.position(0);

		if (paramKMCrypt != null) {
			this.m_vHdr.setSize(41);
			this.m_vHdr.setStatus((short) 255);
		} else {
			this.m_vHdr.setSize(39);
			this.m_vHdr.setStatus((short) 0);
		}
		this.m_repBuf.put(this.m_vHdr.array());

		this.m_repBuf.put(this.m_signature);
		this.m_repBuf.put((byte) 1);
		this.m_repBuf.put((byte) 0);
		this.m_repBuf.put((byte) 32);
		this.m_repBuf.put((byte) 0);
		this.m_repBuf.putInt(i);
		this.m_repBuf.put((byte) 0);
		this.m_repBuf.put((byte) 49);
		this.m_repBuf.put((byte) 32);
		this.m_repBuf.put((byte) Byte.MIN_VALUE);
		this.m_repBuf.put((byte) 2);
		this.m_repBuf.put((byte) 1);
		this.m_repBuf.put((byte) 0);
		this.m_repBuf.put((byte) 0);
		this.m_repBuf.putInt(SeqNum);
		this.m_repBuf.put((byte) 0);
		this.m_repBuf.put((byte) 0);
		this.m_repBuf.put((byte) 0);
		this.m_repBuf.put((byte) 0);
		this.m_repBuf.put((byte) 6);

		if (paramKMCrypt != null) {
			byte[] arrayOfByte1 = new byte[6];
			ByteBuffer localByteBuffer = ByteBuffer.wrap(arrayOfByte1);
			localByteBuffer.order(ByteOrder.LITTLE_ENDIAN);

			localByteBuffer.put(m_btnStatus);
			localByteBuffer.putShort(s1);
			localByteBuffer.putShort(s2);
			localByteBuffer.put(this.m_wheelrotation);

			byte[] arrayOfByte2 = paramKMCrypt.encrypt(arrayOfByte1);

			if (arrayOfByte2 == null) {
				return ABSreport(null);
			}
			this.m_repBuf.put(arrayOfByte2);
		} else {
			this.m_repBuf.put(m_btnStatus);
			this.m_repBuf.putShort(s1);
			this.m_repBuf.putShort(s2);
			this.m_repBuf.put(this.m_wheelrotation);
		}

		int j = 0;
		for (int k = 8; k < 40; k++) {
			j = j + (this.m_repBuf.get(k) & 0xFF) & 0xFF;
		}
		this.m_repBuf.put(19, (byte) -(byte) (j & 0xFF));
		SeqNum += 1;

		return this.m_report;
	}

	public byte[] RELreport(KMCrypt paramKMCrypt) {
		if (paramKMCrypt != null) {
			this.m_vHdr = new IVTPPktHdr((short) 1, 41, (short) 0);

			this.m_report = new byte[49];
		} else {
			this.m_vHdr = new IVTPPktHdr((short) 1, 37, (short) 0);

			this.m_report = new byte[45];
		}

		int i = 5;

		this.m_repBuf = ByteBuffer.wrap(this.m_report);
		this.m_repBuf.order(ByteOrder.LITTLE_ENDIAN);
		this.m_repBuf.position(0);

		if (paramKMCrypt != null) {
			this.m_vHdr.setSize(41);
			this.m_vHdr.setStatus((short) 255);
		} else {
			this.m_vHdr.setSize(37);
			this.m_vHdr.setStatus((short) 0);
		}
		try {
			this.m_repBuf.put(this.m_vHdr.array());

			this.m_repBuf.put(this.m_signature);
			this.m_repBuf.put((byte) 1);
			this.m_repBuf.put((byte) 0);
			this.m_repBuf.put((byte) 32);
			this.m_repBuf.put((byte) 0);
			this.m_repBuf.putInt(i);
			this.m_repBuf.put((byte) 0);
			this.m_repBuf.put((byte) 49);
			this.m_repBuf.put((byte) 32);
			this.m_repBuf.put((byte) Byte.MIN_VALUE);
			this.m_repBuf.put((byte) 2);
			this.m_repBuf.put((byte) 1);
			this.m_repBuf.put((byte) 0);
			this.m_repBuf.put((byte) 0);
			this.m_repBuf.putInt(SeqNum);
			this.m_repBuf.put((byte) 0);
			this.m_repBuf.put((byte) 0);
			this.m_repBuf.put((byte) 0);
			this.m_repBuf.put((byte) 0);
			this.m_repBuf.put((byte) 4);

			if (paramKMCrypt != null) {
				byte[] arrayOfByte1 = new byte[4];
				ByteBuffer localByteBuffer = ByteBuffer.wrap(arrayOfByte1);

				localByteBuffer.put(m_btnStatus);
				localByteBuffer.put((byte) (int) this.m_xDisp);
				localByteBuffer.put((byte) (int) this.m_yDisp);
				localByteBuffer.put(this.m_wheelrotation);

				byte[] arrayOfByte2 = paramKMCrypt.encrypt(arrayOfByte1);

				if (arrayOfByte2 == null) {
					return RELreport(null);
				}

				this.m_repBuf.put(arrayOfByte2);
			} else {
				this.m_repBuf.put(m_btnStatus);
				this.m_repBuf.put((byte) (int) this.m_xDisp);
				this.m_repBuf.put((byte) (int) this.m_yDisp);
				this.m_repBuf.put(this.m_wheelrotation);
			}

			int j = 0;
			for (int k = 8; k < 40; k++) {
				j = j + (this.m_repBuf.get(k) & 0xFF) & 0xFF;
			}
			this.m_repBuf.put(19, (byte) -(byte) (j & 0xFF));
			SeqNum += 1;
			return this.m_report;
		} catch (Exception localException) {
			Debug.out.println(localException);
		}
		return null;
	}

	public byte[] report() {
		if (this.m_MouseMode == ABSOLUTE_MOUSE_MODE) {
			return ABSreport(null);
		}
		return RELreport(null);
	}

	public byte[] encryptedReport(KMCrypt paramKMCrypt) {
		if (this.m_MouseMode == ABSOLUTE_MOUSE_MODE) {
			return ABSreport(paramKMCrypt);
		}

		return RELreport(paramKMCrypt);
	}
}
